import { Request, Response } from 'express';
import { getRepository } from 'typeorm';
import { Category } from '../models/Category';

// 获取分类列表
export const getCategories = async (req: Request, res: Response) => {
  try {
    const categoryRepository = getRepository(Category);
    const categories = await categoryRepository.find({
      order: {
        order: 'ASC'
      }
    });

    res.json(categories);
  } catch (error) {
    res.status(500).json({ message: '获取分类列表失败', error: error.message });
  }
};

// 获取单个分类
export const getCategory = async (req: Request, res: Response) => {
  try {
    const { id } = req.params;
    const categoryRepository = getRepository(Category);
    const category = await categoryRepository.findOne({
      where: { id: Number(id) },
      relations: ['words']
    });

    if (!category) {
      return res.status(404).json({ message: '分类不存在' });
    }

    res.json(category);
  } catch (error) {
    res.status(500).json({ message: '获取分类失败', error: error.message });
  }
};

// 创建分类
export const createCategory = async (req: Request, res: Response) => {
  try {
    const { name, description, order } = req.body;
    const categoryRepository = getRepository(Category);

    const newCategory = categoryRepository.create({
      name,
      description,
      order: order || 0
    });

    await categoryRepository.save(newCategory);
    res.status(201).json(newCategory);
  } catch (error) {
    res.status(500).json({ message: '创建分类失败', error: error.message });
  }
};

// 更新分类
export const updateCategory = async (req: Request, res: Response) => {
  try {
    const { id } = req.params;
    const { name, description, order } = req.body;
    const categoryRepository = getRepository(Category);

    const category = await categoryRepository.findOne({ where: { id: Number(id) } });
    if (!category) {
      return res.status(404).json({ message: '分类不存在' });
    }

    Object.assign(category, {
      name,
      description,
      order
    });

    await categoryRepository.save(category);
    res.json(category);
  } catch (error) {
    res.status(500).json({ message: '更新分类失败', error: error.message });
  }
};

// 删除分类
export const deleteCategory = async (req: Request, res: Response) => {
  try {
    const { id } = req.params;
    const categoryRepository = getRepository(Category);
    const category = await categoryRepository.findOne({
      where: { id: Number(id) },
      relations: ['words']
    });

    if (!category) {
      return res.status(404).json({ message: '分类不存在' });
    }

    if (category.words && category.words.length > 0) {
      return res.status(400).json({ message: '该分类下还有单词，无法删除' });
    }

    await categoryRepository.remove(category);
    res.json({ message: '删除成功' });
  } catch (error) {
    res.status(500).json({ message: '删除分类失败', error: error.message });
  }
};

// 更新分类顺序
export const updateCategoryOrder = async (req: Request, res: Response) => {
  try {
    const { orders } = req.body; // [{ id: number, order: number }]
    const categoryRepository = getRepository(Category);

    for (const { id, order } of orders) {
      await categoryRepository.update(id, { order });
    }

    res.json({ message: '更新顺序成功' });
  } catch (error) {
    res.status(500).json({ message: '更新分类顺序失败', error: error.message });
  }
}; 